package com.example.experiment1;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.experiment1.date.ShopItem;

import java.util.ArrayList;

public class Hello00363Activity extends AppCompatActivity  {



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_hello00363);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (View v, WindowInsetsCompat insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        ArrayList<ShopItem> items = new ArrayList<ShopItem>();
        for (int i = 0; i < 100; ++i) {
            items.add(new ShopItem("item"+i,0.2+i,R.drawable.compute2));
        }
        RecyclerView shopRecyclerView = findViewById(R.id.recyclerview_main_list);
        shopRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        shopRecyclerView.setAdapter( new ShopItemAdapter(items));
    }
    public static class ShopItemAdapter extends RecyclerView.Adapter<ShopItemAdapter.ViewHolder> {

        private final ArrayList<ShopItem> localDataSet;

        /**
         * Provide a reference to the type of views that you are using
         * (custom ViewHolder)
         */
        public static class ViewHolder extends RecyclerView.ViewHolder {
            private final TextView textViewItemView;
            private final TextView textViewItemPrice;
            private final ImageView imageViewItem;

            public ViewHolder(View viewRowRoot) {
                super(viewRowRoot);
                // Define click listener for the ViewHolder's View

                textViewItemView =  viewRowRoot.findViewById(R.id.textView_shop_item_name);
                textViewItemPrice= viewRowRoot.findViewById(R.id.textView_shop_item_price);
                imageViewItem = viewRowRoot.findViewById(R.id.image_view_item);
            }

            public TextView getTextViewItemView() {
                return textViewItemView;
            }

            public TextView getTextViewItemPrice() {
                return textViewItemPrice;
            }

            public ImageView getImageViewItem() {
                return imageViewItem;
            }
        }

        /**
         * Initialize the dataset of the Adapter
         *
         * @param dataSet String[] containing the data to populate views to be used
         * by RecyclerView
         */
        public ShopItemAdapter(ArrayList<ShopItem> dataSet) {
            localDataSet = dataSet;
        }

        int ViewHolderCount = 0;
        // Create new views (invoked by the layout manager)
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            // Create a new view, which defines the UI of the list item
            View viewRowRoot = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.shop_item_row, parent, false);

            ViewHolderCount++;

            return new ViewHolder(viewRowRoot);
        }

        // Replace the contents of a view (invoked by the layout manager)
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, final int position) {

            // Get element from your dataset at this position and replace the
            // contents of the view with that element
            viewHolder.getTextViewItemView().setText(localDataSet.get( position).getName());
            viewHolder.getTextViewItemPrice().setText(String.format("%s", localDataSet.get(position).getPrice()));
            viewHolder.getImageViewItem().setImageResource(localDataSet.get(position).getDrawableId());
        }

        // Return the size of your dataset (invoked by the layout manager)
        @Override
        public int getItemCount() {
            return localDataSet.size();
        }
    }
}